#include "subjectcollection.h"
#include "ui_subjectcollection.h"

subjectcollection::subjectcollection(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::subjectcollection)
{
    ui->setupUi(this);

    // 创建按钮组
    m_subjectButtonGroup = new QButtonGroup(this);

    m_subjectButtonGroup->setExclusive(true);
    // 将按钮添加到按钮组中
    for (int idx = 1; idx <= m_buttonNums; idx++)
    {
        QPushButton * button = findChild<QPushButton*>(QString("select%1").arg(idx));
        if (button != NULL)
        {
            button->setStyleSheet("QPushButton {"
                                 "background-color: #434343;"
                                 "color: white;"
                                 "border-radius: 5px;"
                                 "padding: 10px;"
                                 "}"
                                 "QPushButton:hover {"
                                 "background-color: #2980b9;"  // 悬停时的背景颜色
                                 "}"
                                 "QPushButton:pressed {"
                                 "background-color: #1abc9c;"  // 按下时的背景颜色
                                 "}");
            m_subjectButtonGroup->addButton(button, idx);
        }
    }
    int size = m_subjectButtonGroup->buttons().size();
    /* 在按钮上设置题号 */
    for (int idx = 1; idx <= size; idx++)
    {
        QString text = QString("%1").arg(idx);
        m_subjectButtonGroup->button(idx)->setText(text);
    }

    /* 信号和槽绑定 */
    connect(m_subjectButtonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked), this, &subjectcollection::buttonGroupSlot);

}

subjectcollection::~subjectcollection()
{
    delete ui;
}

// 设置按钮使用
void subjectcollection::setWhichAnswered(int idx)
{
    m_subjectButtonGroup->button(idx)->setStyleSheet(
                         "QPushButton {"
                         "background-color: #ffa810;"
                         "color: white;"
                         "border-radius: 5px;"
                         "padding: 10px;"
                         "}"
                         );
}

// 设置当前按钮正在使用中
void subjectcollection::setCurrentBtnUsing(int idx)
{
    m_subjectButtonGroup->button(idx)->setStyleSheet(
                         "QPushButton {"
                         "background-color: #48d1cc;"
                         "color: white;"
                         "border-radius: 5px;"
                         "padding: 10px;"
                         "}"
                         );
}

// 设置按钮没有被使用
void subjectcollection::setWhichNoUsed(int idx, QString styleSheet)
{
    m_subjectButtonGroup->button(idx)->setStyleSheet(styleSheet);
}

int subjectcollection::getButtonNums()
{
    return  m_buttonNums;
}

// 按钮组哪个按钮被按下
void subjectcollection::buttonGroupSlot(int idx)
{
    emit whichOneClick(idx);
}
